/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.shwread.android.qysw.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.shwread.android.qysw10000182.R;

import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
import org.geometerplus.fbreader.book.Bookmark;
import org.geometerplus.fbreader.book.HighlightingStyle;
import org.geometerplus.zlibrary.core.util.ZLColor;

import logic.constants.DefaultConsts;
import logic.util.Util;


/**
 * @Project： Lqyd_Android
 * @Title：RemarkActivity
 * @Description：
 * @Author：wayne
 * @Date：2014年8月25日下午4:25:42
 */
public class NotesActivity
        extends
        BaseSimpleActivity
        implements
        View.OnClickListener
{
	public static final String                        EXISTING_BOOKMARK_KEY = "existing.bookmark";
	
	private Button                                    mBackBtn;
	private TextView                                  mTitleTv;
	private Button		                              mSubmit;
	private EditText                                  mRemarkText;
	private final BookCollectionShadow                myCollection          = new BookCollectionShadow();
	private boolean                                   myExistingBookmark;
	public static Bookmark                            myBookmark;
	
	private Handler                                   receivedHandler       = new Handler()
	                                                                        {
		                                                                        public void handleMessage(Message msg) {
			                                                                        switch (msg.what) {
																						case DefaultConsts.UPDATEUI_ADD_BOOK_ANNOATION:
																							
																							final HighlightingStyle highStyle = new HighlightingStyle(3, "", new ZLColor(7512015));
																							myCollection.bindToService(NotesActivity.this, new Runnable()
																							{
																								public void run() {
																									myBookmark.setStyleId(highStyle.Id);
																									myCollection.saveBookmark(myBookmark);
																								}
																							});
																							
																							NotesActivity.this.finish();
																							break;
																					}
																				};
	                                                                        };
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.book_notes_edit);
		
		myCollection.bindToService(this, new Runnable()
		{
			
			@Override
			public void run() {
				myExistingBookmark = getIntent().getBooleanExtra(EXISTING_BOOKMARK_KEY, false);
				if (myBookmark == null)
				{
					finish();
					return;
				}
				else
				{
					if (myBookmark.getBookNote() != null && !"null".equals(myBookmark.getBookNote()))
					{
						mRemarkText.setText(myBookmark.getBookNote());
					}
				}
			}
		});
		
		initView();
		initDate();
		addListener();
		initConst();
		Util.showInputMethodManager(this);
	}
	
	void initView() {
		mBackBtn = (Button) findViewById(R.id.common_header_back);
		mTitleTv = (TextView) findViewById(R.id.common_header_name);
		mSubmit = (Button) findViewById(R.id.common_header_commit);
		mRemarkText = (EditText) findViewById(R.id.book_notes_edit_notes_et);
	}
	
	void initDate() {
		mBackBtn.setVisibility(View.VISIBLE);
		mTitleTv.setText("笔记");
		mSubmit.setText("提交");
		mSubmit.setVisibility(View.VISIBLE);
		mRemarkText.setHint("添加您的阅读笔记吧（最多1000字）");
		if (!Util.isEmpty(myBookmark.getBookNote())) {
			mRemarkText.setText(myBookmark.getBookNote());
			mRemarkText.setSelection(myBookmark.getBookNote().length());//将光标移至文字末尾
		}
	}
	
	void addListener() {
		mBackBtn.setOnClickListener(this);
		mSubmit.setOnClickListener(this);
	}
	
	void initConst() {
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		
	}
	
	public Handler getUIHandler() {
		return receivedHandler;
	}
	
	@Override
	public void onBackPressed() {
		Util.hideInputMethodManager(this, mRemarkText);
		super.onBackPressed();
	}
	
	@Override
	protected void onDestroy() {
		myCollection.unbind();
		super.onDestroy();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				Util.hideInputMethodManager(this, mRemarkText);
				finish();
				break;
			case R.id.common_header_commit:
				saveBookRemark();
				break;
		}
	}
	
	void saveBookRemark() {
		
		String remarkText = mRemarkText.getText().toString();
		if ( Util.isNotEmpty(remarkText)) {
			myBookmark.setBookNote(remarkText);
			myBookmark.setStyleId(3);
			myBookmark.setMyTypeID(3);//笔记
			myCollection.bindToService(NotesActivity.this, new Runnable()
			{
				public void run() {
					myCollection.saveBookmark(myBookmark);
				}
			});
			Util.hideInputMethodManager(this, mRemarkText);
			Util.showToast(this, "笔记已保存");
			finish();
		} else {
//			myBookmark.setStyleId(2);
//			myBookmark.setMyTypeID(2);//高亮
			Util.showToast(this, "请输入笔记");
			
		}
		
	}
	
}
